// pages/vote/vote.js
import { Inquiry } from '../../model/inquiry.js'
const inquiry = new Inquiry();
Page({

    /**
     * 页面的初始数据
     */
    data: {
      indicatorDots:false,
      id:'',
      imgUrls:[],
      questionList:[],
      inquirytit:{},
      questionId:'',//问题ID
      classId:'',//活动id
      optionId:'',//IDstring
      inquiry:{},
      questionOptionName:'请简答'
    },
    // 单选
    radioChange(e){
      console.log(e.currentTarget.dataset.index)
      var arr = []
      arr.push(e.detail.value)
      var list = this.data.questionList
      // var inquiryIndex=this.dquestionList
      for(var i in list){
        if (list[i].id == e.currentTarget.dataset.item.id){
          list[i].anslist = arr
          for(var d in list[i].optionVoList){
            list[i].optionVoList[d].isChose =0
            if(list[i].optionVoList[d].id == e.detail.value){
              list[i].optionVoList[d].isChose =1
            }
          }
        }
      }
      this.setData({
        questionList: list
      })
    },
    // 选择
    checkboxChange: function (e) {
      console.log(e)
      var list = this.data.questionList
      var t = e.currentTarget.dataset.index
      for (var i in list) {
        if (list[i].id == e.currentTarget.dataset.item.id) {
          // for (var t in list[i].optionVoList){
            list[i].optionVoList[t].checked = !list[i].optionVoList[t].checked
            if (list[i].optionVoList[t].checked) {
              list[i].anslist.push(list[i].optionVoList[t].id)
              list[i].optionVoList[t].isChose=1
            }
            else {
              for (var j in list[i].anslist) {
                if (list[i].optionVoList[t].id == list[i].anslist[j]) {
                  list[i].anslist.splice(j, 1)
                  list[i].optionVoList[t].isChose=0
                }
              }
            }
          // }
        }
      }
      this.setData({
        questionList: list
      })
    },
    // 简答
    bytext(e){
      console.log(e)
      var list = this.data.questionList
      for (var i in list) {
        if (list[i].id == e.currentTarget.dataset.item.id) {
          list[i].anstext = e.detail.value
        }
      }
      this.setData({
        questionList: list
      })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      this.setData({
        id:options.id
      })
      this.inquiryDetail()
    },
    // navBar返回
    handlerGobackClick() {
      const pages = getCurrentPages();
      if (pages.length >= 2) {
        wx.navigateBack({
          delta: 1
        });
      } else {
        wx.navigateTo({
          url: '/pages/firstpage/firstpage'
        });
      }
    },
    //问卷调查接口
    inquiryDetail(){
      var obj={id:this.data.id}
      inquiry.investigation_detail(obj).then(res=>{
        console.log(res)
        var imgUrls=this.data.imgUrls 
        for(var i=0;i<res.data.picList.length;i++){
          imgUrls.push(res.data.picList[i])//轮播图集合
        }
        this.setData({
          imgUrls:imgUrls,
          
        })
        

        for(var j=0;j<res.data.questionInfoList.length;j++){
          // questionList.push(res.data.questionInfoList[j])//问题集合
          res.data.questionInfoList[j].isChoses = 0
          if(res.data.questionInfoList[j].type!=3){
            res.data.questionInfoList[j].anslist = []
            for(var k=0;k<res.data.questionInfoList[j].optionVoList.length;k++){
              res.data.questionInfoList[j].optionVoList[k].checked=false
              if(res.data.questionInfoList[j].optionVoList[k].isChose ==1){
                res.data.questionInfoList[j].isChoses = 1
              }
            }
          }
          else{
            res.data.questionInfoList[j].anstext = ''
          }
          if(res.data.questionInfoList[j].questionOptionName!=null && res.data.questionInfoList[j].questionOptionName!=''){
            this.setData({
              questionOptionName:res.data.questionInfoList[j].anstext
            })

          }
        }
        this.setData({
          inquirytit:res.data.entity,
          // optionVoList:res.data.questionInfoList,//小列表
          questionList:res.data.questionInfoList,//大列表
          classId: res.data.questionInfoList[0].classId,
          inquiry:res.data
        })
        console.log(this.data.inquirytit)
        console.log(this.data.optionVoList)
        console.log(this.data.questionList)
      }).catch(error=>{
        console.log(error)
      })
    },

    //问卷调查参数
    //报名参数
    onsubmits(){
      var that=this
      var obj = []
      for (var i in that.data.questionList){
        var msg ={}
        if (that.data.questionList[i].type ==1){
          //单选
          msg = {
            questionInfoId: that.data.questionList[i].id,
            optionIds: that.data.questionList[i].anslist,
            remark:'',
          }
          if (that.data.questionList[i].anslist.length == 0){
            wx.showToast({
              title: '请作答全部选择题',
              icon:'none'
            })
            return false
          }
          obj.push(msg)
        }
        else if (that.data.questionList[i].type == 2){
          //多选
          msg = {
            questionInfoId: that.data.questionList[i].id,
            optionIds: that.data.questionList[i].anslist,
            remark: '',
          }
          if (that.data.questionList[i].anslist.length == 0) {
            wx.showToast({
              title: '请作答全部选择题',
              icon: 'none'
            })
            return false
          }
          obj.push(msg)
        }
        else if(that.data.questionList[i].type == 3){
          //简答
          msg = {
            questionInfoId: that.data.questionList[i].id,
            optionIds: [],
            remark: that.data.questionList[i].anstext,
          }
          // if (that.data.questionList[i].anstext == '') {
          //   wx.showToast({
          //     title: '请作答简答题',
          //     icon: 'none'
          //   })
          //   return false
          // }
          obj.push(msg)
        }
      }
      
      var parmes = {
        id:that.data.id,
        json:JSON.stringify(obj)
      }
      wx.showModal({
        title: '提示',
        content: '您确定要提交吗',
        success (res) {
          if (res.confirm) {
            console.log('用户点击确定')
            that.apply_yes(parmes)
            console.log(parmes)
            wx.showToast({
              title: '报名成功',
      
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      
    },

    //报名
    apply_yes(parmes){
      inquiry.join_investigation(parmes).then(res=>{
        console.log(res)
        var inquiry=this.data.inquiry
        inquiry.isJoin=1

        var questionList =this.data.questionList
        for(var i in questionList){
          if(questionList[i].type != 3){
            questionList[i].isChoses =1
          }
        }
        this.setData({
          inquiry:inquiry,
          questionList:questionList
        })
        if(res.code==0 && res.data.notice!=''){
          wx.showModal({
            title: '提示',
            content: res.data.notice,
            showCancel:false,
            success (res) {
              if (res.confirm) {
              console.log('用户点击确定')
                wx.navigateBack({
                  complete: (res) => {},
                  delta:1
                })
              }
            }
          })
          
        }else if(res.code==1){
          wx.showModal({
            title: '提示',
            content: res.msg,
            success (res) {
              if (res.confirm) {
              console.log('用户点击确定')
                wx.navigateBack({
                  complete: (res) => {},
                  delta:1
                })
              } else if (res.cancel) {
              console.log('用户点击取消')
              }
            }
          })
          
        }
      }).catch(error=>{
        console.log(error)
      })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
  
    },
  
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
  
    },
  
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
  
    },
  
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
  
    },
  
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
  
    },
  
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
  
    },
  
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
  
    }
  })